/////////////////////////////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2009 Eric Bowen
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and 
// associated documentation files (the "Software"), to deal in the Software without restriction, including 
// without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
// copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the 
// following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial 
// portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT 
// LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN 
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
/////////////////////////////////////////////////////////////////////////////////////////////////////
// Author's blog: http://blog.scrappydog.com
// Updated source code available:  http://scrappydb.codeplex.com
/////////////////////////////////////////////////////////////////////////////////////////////////////

using System.Web.Caching;
using NLog;
using ScrappyDB.Utilities;

namespace ScrappyDB.BaseClasses
{
    public class SdbEntityWithLocation : SdbEntity
    {
        private static readonly Logger Log = LogManager.GetCurrentClassLogger();
        public double Distance;

        public double Latitude { get; set; }
        public double Longitude { get; set; }

        //this is a temporary value that only has meaning in context of proximity searches, and is not saved to the database

        public SdbCollection<T> FindNearbyEntities<T>(int distance, bool isMetric)
            where T : SdbEntityWithLocation, new()
        {
            SdbCollection<T> result = null;
            if (WebCache != null)
                result = (SdbCollection<T>) WebCache.Get(NearbyCacheKey(distance, isMetric));

            if (result != null)
                return result;

            result = LatLonProximity.FindEnititiesByProximity<T>(Latitude, Longitude, distance, isMetric, WebCache);

            if (WebCache != null)
                WebCache.Add(NearbyCacheKey(distance, isMetric), result, null, Configuration.CacheTimeout,
                             Cache.NoSlidingExpiration,
                             CacheItemPriority.Default, null);

            Log.Info("CACHE ADD: FindNearbyEntities key:  {0}", NearbyCacheKey(distance, isMetric));

            return result;
        }

        private string NearbyCacheKey(int distance, bool isMetric)
        {
            return ItemName + "-" + distance + "-" + isMetric;
        }
    }
}